//
// Created by zhulingfeng on 2020/2/26.
//

#include "sort_test.hpp"
#include <algorithm>
SortTest::SortTest() { Init(100000, 1000000, 0, 10); }
void SortTest::Init(int fix_array_length, int max_array_length,
                    int max_array_value, int num_test_arrays) {
  std::default_random_engine e;
  std::uniform_int_distribution<unsigned> random_size(1, max_array_length);
  std::uniform_int_distribution<int> random_value(0, max_array_value);

  /** Set unsorted data. **/
  unsorted_vectors_.clear();
  // The empty input test.
  unsorted_vectors_.emplace_back();
  unsorted_lists_.push_back(nullptr);
  unsorted_vectors_.resize(num_test_arrays + 1);
  unsorted_lists_.resize(num_test_arrays + 1);
  for (int i = 1; i < unsorted_vectors_.size(); i++) {  // start with 1 for th
    // empty input.
    int size = fix_array_length;
    if (fix_array_length == -1) {
      size = random_size(e);
    }
    auto &vector = unsorted_vectors_[i];
    vector.resize(size);
    ListNode dummy;
    // Iterate reversely to make list elements in the same order with vector.
    for (auto j = size - 1; j > 0; j--) {
      vector[j] = random_value(e);
      dummy.Insert(vector[j]);
    }
    unsorted_lists_[i] = dummy.next;
  }
  /** Set sorted data. **/
  sorted_vectors_ = unsorted_vectors_;
  for (auto &v : sorted_vectors_) {
    std::sort(v.begin(), v.end());
  }
  sorted_lists_.resize(unsorted_lists_.size());
  for (int i = 1; i < sorted_vectors_.size(); i++) {  // start with 1 for
    // the empty input.
    ListNode dummy;
    // Iterate reversely to make list elements in the same order with vector.
    for (auto j = sorted_vectors_[i].size() - 1; j > 0; j--) {
      dummy.Insert(sorted_vectors_[i][j]);
    }
    sorted_lists_[i] = dummy.next;
  }
}
SortTest::~SortTest() {
  ListNode dummy;
  for (auto &unsorted_list : unsorted_lists_) {
    dummy.next = unsorted_list;
    dummy.DeleteList();
  }
  for (auto &sorted_list : sorted_lists_) {
    dummy.next = sorted_list;
    dummy.DeleteList();
  }
}
